load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_cc//cc:defs.bzl", "cc_library")

string_flag(
    name = "ssl",
    build_setting_default = "no_ssl",
    values = [
        "no_ssl",
        "openssl",
        "boringssl",
    ],
    visibility = ["//visibility:public"],
)

config_setting(
    name = "no_ssl",
    flag_values = {":ssl": "no_ssl"},
)

config_setting(
    name = "openssl",
    flag_values = {":ssl": "openssl"},
)

config_setting(
    name = "boringssl",
    flag_values = {":ssl": "boringssl"},
)

write_file(
    name = "src",
    out = "boost.asio.src.cpp",
    content = select({
        ":no_ssl": [
            "#include <boost/asio/impl/src.hpp>",
        ],
        "//conditions:default": [
            "#include <boost/asio/impl/src.hpp>",
            "#include <boost/asio/ssl/impl/src.hpp>",
        ],
    }),
)

cc_library(
    name = "boost.asio",
    srcs = [":src"] + select({
        ":no_ssl": [
            "include/boost/asio/impl/src.hpp",
        ],
        "//conditions:default": [
            "include/boost/asio/impl/src.hpp",
            "include/boost/asio/ssl/impl/src.hpp",
        ],
    }),
    hdrs = glob(
        [
            "include/**/*.hpp",
            "include/**/*.ipp",
        ],
        exclude = [
            "include/boost/asio/impl/src.hpp",
            "include/boost/asio/ssl/impl/src.hpp",
        ],
    ),
    defines = ["BOOST_ASIO_SEPARATE_COMPILATION"],
    includes = ["include"],
    linkopts = select({
        "@platforms//os:windows": [
            "-DEFAULTLIB:ws2_32",
            "-DEFAULTLIB:mswsock",
        ],
        "//conditions:default": [
            "-lpthread",
        ],
    }),
    visibility = ["//visibility:public"],
    deps = [
        "@boost.align",
        "@boost.array",
        "@boost.assert",
        "@boost.bind",
        "@boost.chrono",
        "@boost.config",
        "@boost.context",
        "@boost.core",
        "@boost.coroutine",
        "@boost.date_time",
        "@boost.exception",
        "@boost.function",
        "@boost.regex",
        "@boost.smart_ptr",
        "@boost.system",
        "@boost.throw_exception",
        "@boost.type_traits",
        "@boost.utility",
    ] + select({
        ":openssl": ["@openssl//:ssl"],
        ":boringssl": ["@boringssl//:ssl"],
        ":no_ssl": [],
    }),
)
